from loaddata import mgDB
from collections import Counter
'''
统计最大间隔
'''
def tj(dx):
    # 查看一下多少次中奖一次，间隔多少次
    r = []
    a = 0
    for d in dx:
        if d == 0:
            a += 1
        else:
            r.append(a)
            a = 0
    dxres = Counter(dx)
    mzl = round(dxres.get(1)/len(dx),4)


    print("统计：", dxres,"命中率：",mzl)
    print("中奖记录：", dx)
    print("最大间隔：", max(r))
    res = Counter(r)
    # 计算平均间隔
    print("平均间隔：", round(sum(r)/len(r),2))
    print("统计：", res)
    print("间隔多久中：", r)

# 统计让1球的胜平负，赔率小于1.5的命中概率
def tongji(ds):
    zhong = 0
    dx = []
    for d in ds:
        z = 0
        if d[0].get("zhong") == 1:
            zhong +=1
            z = 1
        dx.append(z)
    r = round(zhong/len(ds),2)
    print(r)
    tj(dx)


def selectds15():
    ds = mgDB.get_jincai(None)
    selectds = []
    for ddd in ds:
        fixedBonus = ddd.get("fixedBonus")
        hhadList = fixedBonus.get("oddsHistory").get("hhadList")
        if hhadList:
            hhad = hhadList[-1]
            goalLine = hhad.get("goalLine")
            a = float(hhad.get("a"))     # 负
            h = float(hhad.get("h"))    # 胜
            d = float(hhad.get("d"))    # 平
            ahd = [{"key":"h","v":h,"zhong":0},{"key":"d","v":d,"zhong":0},{"key":"a","v":a,"zhong":0}]
            tt = sorted(ahd, key=lambda e: e["v"], reverse=False)
            minp = tt[0]
            temp= 1.45
            if goalLine == "-1" or goalLine == "+1":
                if minp.get("v")<temp:
                    # 判断是否中奖
                    matchResultList  = fixedBonus.get("matchResultList")
                    if len(matchResultList)>0:
                        matchResult = matchResultList[0]
                        combination = matchResult.get("combination").lower()
                        if minp.get("key") == combination:
                            tt[0]["zhong"] = 1
                        selectds.append(tt)

    return selectds


if __name__ == '__main__':
    selectds = selectds15()
    tongji(selectds)